<?php
use think\facade\Session;
use think\facade\cookie;
use think\facade\Db;
/**
 * 修改扩展配置文件
 * @param array  $arr  需要更新或添加的配置
 * @param string $file 配置文件名(不需要后辍)
 * @return bool
 */
 
               
function extraconfig($array = [], $file = '')
{
    if (is_array($array)) {
        $filename = $file .'.php';
        $filepath = config_path() . $filename;
        if (!file_exists($filepath)) {
            $list = "<?php return [];";
            file_put_contents($filepath, $list);
        }
        $list = include $filepath;
        foreach ($array as $key => $value) {
            $list[$key] = $value;
        }
        $time = date('Y/m/d H:i:s');
        $data = "<?php\r\n/**\r\n * 九月科技（http://www.ztuc.cn）.\r\n * $time\r\n */\r\nreturn [\r\n";
        foreach ($list as $key => $value) {
            $data .= "\t'$key' => '$value',";
            $data .= "\r\n";
        }
        $data .= '];';
        file_put_contents($filepath, $data);
        
        return true;
    } else {
        return false;
    }
}
 
/**
 * 管理员操作日志
 * @param string $type 操作类型 login add edit view delete
 * @param int    $param_id 操作类型
 * @param array  $param 提交的参数
 * @param subject  $param  操作主题add_log('upload', $data['user_id'], $data,'文件');
 */
function add_admin_log($type, $subject='')
{
  $action = '未知操作';
  $type_action = get_config('log.get_action');
   if($type_action[$type]){ 
        $action = $type_action[$type];
    }
      $login_admin = Db::name('Admin')->where(['token' => cookie('token')])->find();
    
    $data = array();
    $data['param_id'] = isset(explode("=", substr(request()->url(), 0, 1500))[1])?explode("=", substr(request()->url(), 0, 1500))[1]:'0';
    if(empty(AdminId())){
    $data['username'] = $login_admin['username'];
    }else{
    $data['username'] = AdminId(); 
    }
    $data['url'] = substr(request()->url(), 0, 1500);
  
    $data['type'] = $type;
    $data['action'] = $action;
    
    if(empty($subject)){
            $data['subject'] = '系统';
        }else{
            $data['subject'] = $subject;
        }
    
 
    $data['content'] = $data['username'] . ', ' . '在 ' . date('Y-m-d H:i:s') . $data['action'] . ' 了' . $data['subject'];
  
    $data['ip'] = app('request')->ip();
    $data['create_time'] = time();
   \app\lnadmin\model\admin\AdminLog::insert($data);
}
   